Omplus.base.define(Omplus.controllers.Controller, {
    omplusName:"mediaController"
});

Omplus.base.define(Omplus.models.Model, {
    omplusName:"MediaTextsSearch",
    controller:"mediaController",
    value:{
        "asset_name":{
            omplusType:"String"
        },
        "image_name":{
            omplusType:"String"
        },
        delta:{
            omplusType:"Number",
            value:5
        },
        start:{
            omplusType:"Number",
            value:0
        }
    }
});

Omplus.models.MediaTextsSearchModelProto = Omplus.base.extend(Omplus.models.Object, {
    omplusType:"MediaTextsSearchModelProto",
    value:{
        "asset_name":{
            omplusType:"String"
        },
        "image_name":{
            omplusType:"String"
        },
        "image_url":{
            omplusType:"String"
        }
    }
});

Omplus.base.define(Omplus.models.Model, {
    omplusName:"MediaTextsSearchResults",
    controller:"mediaController",
    value:{
        assets:{
            omplusType:"Array",
            omplusProto:"MediaTextsSearchModelProto"
        },
        total:{
            omplusType:"Number"
        }
    }
});


Omplus.base.define(Omplus.views.ManagementPanel, {
    omplusName:"mediaPanel",
    controller:"mediaController",
    renderTo:"managements",
    models:{
        search:"MediaTextsSearch",
        searchResult:"MediaTextsSearchResults",
        searchResultList:"assets"
    },
    views:{
        search:[
            {
                model:"MediaTextsSearch.asset_name",
                labelModel:"Assets.list.asset_name_label.localValue",
                inputType:"TextInput"
            },
            {
                model:"MediaTextsSearch.image_name",
                labelModel:"Assets.list.image_name_label.localValue",
                inputType:"TextInput"
            },
            {
                // Per page
                inputType:"Select",
                model:"PerPages",
                showModel:"number",
                current:"forTransactionPlayers",
                controller:"languageController",
                labelModel:"Assets.list.label_per_page.localValue",
                perPage:true
            },
            {
                // Search button
                inputType:"Button",
                controller:"languageController",
                model:"Assets.list.label_search_button.localValue",
                cssClasses:["search-button"],
                labelWidth:"0%",
                inputWidth:"100%",
                searchButton:true
            },
            {
                // Add button
                omplusType:"Button",
                omplusName:"AddAssetButton",
                controller:"languageController",
                model:"Assets.list.label_add.localValue",
                click:function () {
                    Omplus.globals.getObject("AddNewDigitalAsset").show();
                }
            }
        ],
        searchResult:[
            {
                labelModel:"Assets.list.asset_name_label.localValue",
                omplusType:"Label",
                width:"280px",
                model:"asset_name"
            },
            {
                labelModel:"Assets.list.image_name_label.localValue",
                omplusType:"Label",
                width:"280px",
                model:"image_name"
            },
            {
                labelModel:"Assets.list.image_name_label.localValue",
                omplusType:"Image",
                width:"140px",
                model:"image_url"
            },
            {
                omplusType:"Button",
                model:"Assets.list.label_delete.localValue",
                width:"250px",
                controller:"languageController",
                click:function () {
                    var asset = $(this).closest(".tableRow").find("label:first").text();
                    Omplus.globals.getObject("AssetEditDigitalModel.for_deletion").setValue(asset);
                    Omplus.globals.getObject("DeleteDigitalAsset").show();
                }
            }
        ]
    },
    tableProto:{
        omplusType:"Panel",
        components:[
            {
                omplusName:"tableColumnNameLabels",
                omplusType:"Panel",
                components:[],
                cssClasses:[
                    "table-header"
                ]
            },
            {
                omplusType:"DinamicTable",
                orientation:"vertical",
                rowsPerPage:5,
                rowHeight:"100px",
                rowCellWidths:[]
            }
        ]
    }
});

Omplus.base.define(Omplus.models.Model, {
    omplusName:"DigitalAssetModel",
    controller:"mediaController",
    value:{
        new_asset:{
            omplusType:"String"
        }
    },
    actions:[
        {
            omplusType:"Get",
            omplusName:"addAsset",
            proxy:{
                omplusType:"AjaxProxy"
            },
            requestModel:"DigitalAssetModel"
        },
        {
            omplusType:"Get",
            omplusName:"deleteAsset",
            proxy:{
                omplusType:"AjaxProxy"
            },
            requestModel:"AssetEditDigitalModel",
            callback:function () {
                $("#mediaPanel_searchButton_input").click();
            }
        }
    ]
});

Omplus.base.define(Omplus.views.Dialog, {
    omplusName:"AddNewDigitalAsset",
    width:"500px",
    height:"300px",
    components:[
        {
            omplusType:"TempUploader",
            omplusName:"UploadNewDigitalAsset",
            controller:"mediaController",
            model:"DigitalAssetModel.new_asset",
            callback:function () {
                console.log(Omplus.globals.getObject("DigitalAssetModel"));
            }
        }
    ],
    confirmClick:function () {
        Omplus.globals.getObject("DigitalAssetModel").getAction("addAsset").run();
        Omplus.globals.getObject("AddNewDigitalAsset").hide();
    }
});

Omplus.base.define(Omplus.models.Model, {
    omplusName:"AssetEditDigitalModel",
    controller:"contentController",
    value:{
        for_deletion:{
            omplusType:"String"
        }
    }
});

Omplus.base.define(Omplus.views.Dialog, {
    omplusName:"DeleteDigitalAsset",
    width:"500px",
    height:"300px",
    components:[
        {
            omplusType:"Label",
            omplusName:"deleteDigitalAssetQuestion",
            model:"Assets.list.delete_asset_question_label.localValue",
            controller:"languageController"
        }
    ],
    confirmClick:function () {
        Omplus.globals.getObject("DigitalAssetModel").getAction("deleteAsset").run();
        Omplus.globals.getObject("DeleteDigitalAsset").hide();
    }
});